package com.clerkq.CircleList.view
{
	import com.clerkq.CircleList.data.ThumbHVO;
	import com.clerkq.CircleList.event.CircleListEvent;
	import com.content.model.Collection.ItemGroup;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.Geometry;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class ThumbListH extends UIObject implements IDisposable
	{
		// Display number
		private var _visibleNum:uint;
		private var _activeThumbIndex:int;
		// Total number for how many buttons should be placed on the left/right side
		private var _leftNum:uint;
		private var _rightNum:uint;
		// 显示区域内(mask内)最左和最右的thumb的seq号(不是index号)
		private var _leftBoundSeq:int;
		private var _rightBoundSeq:int;
		// Top/Bottom extreme position for button gallery
		private var _leftLimPos:Number;
		private var _rightLimPos:Number;
		
		public var left:Number = 0;
		public var right:Number = 0;
		private var _thumbPipe:Array;
		
		private var _thumbSpace:int;
		private var _thumbWidth:int;
		
		public function ThumbListH(visibleNum:int, thumbSpace:int, thumbWidth:int)
		{
			super();
			_thumbSpace = thumbSpace;
			_thumbWidth = thumbWidth;
			
			_visibleNum = visibleNum;
			_thumbPipe = new Array();
		}
		
		public function MoveList(distance:Number = 0):void{
			var direct:String;
			if(distance >= 0){
				direct = "left";
        	}else if(distance < 0){
				direct = "right";
        	}
            	
			var thumbNum:uint = _thumbPipe.length;
			if(direct == "left"){
				
				var temp:Number = (thumbNum - _visibleNum)/2.0;
 				var temp2:Number = Math.floor((thumbNum - _visibleNum)/2.0);
				_leftNum = temp < 0 ? 0:temp2;
				// If the total number is even, the rightNumber should be larger than the leftNumber.
				_leftLimPos = _thumbSpace * _leftNum * (-1);			
				
				// Total thumbNails number is Even
				if( !Boolean(thumbNum % 2) ){
					// Odd
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum;
					}else{
						_rightNum = _leftNum + 1;					
					}				
				}else{
					// Even
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum + 1;
					}else{
						_rightNum = _leftNum;
					}
				}			
			}else if(direct == "right"){
				
				var temp3:Number = Math.ceil((thumbNum - _visibleNum)/2.0);
 				var temp4:Number = 0;
				_leftNum = temp3 < 0 ? temp4:temp3;
				
				// If the total number is even, the rightNumber should be larger than the leftNumber.
				_leftLimPos = _thumbSpace * (_leftNum ) * (-1);			
				
				if( !Boolean(thumbNum % 2) ){
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum;
					}else{
						_rightNum = _leftNum - 1;					
					}				
				}else{
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum - 1;
					}else{
						_rightNum = _leftNum;
					}
				}			
			}
			_rightLimPos = _thumbSpace * (_rightNum + _visibleNum );	
			
			// local variable
			var i:uint;						
			
			// 1. caculate	( 这里算出 groupLeft/groupRight 是为了判断移动后会不会出界)		
			var groupLeft:Number;			
			var groupRight:Number;
			groupLeft = (_thumbPipe[0].x - _thumbWidth/2) + distance;
			groupRight = groupLeft + _thumbSpace * (thumbNum);
			
			// Number of buttons that over bound
			var overNumber:Number = 0;			
			
			var headThumbX:Number = _thumbPipe[0].x;//(_thumbs[0].y + Thumb.HEIGHT/2);
			if(direct == "left"){
				if( groupRight >= _rightLimPos ){
					// over right				
					overNumber = Math.ceil((groupRight - _rightLimPos)/_thumbSpace);
					//trace("Over Right Number is : "+ overNumber);
					for( i = 0; i < overNumber; i++ ){
						var tempBottom:ThumbH = _thumbPipe.pop();
						_thumbPipe.unshift(tempBottom);
					}				
				}
			}
			if(direct == "right"){
				if( groupLeft < _leftLimPos ){
					// over top
					overNumber = Math.ceil((_leftLimPos - groupLeft)/_thumbSpace);
					for( i = 0; i < overNumber; i++ ){
						var tempTop:ThumbH = _thumbPipe.shift();
						_thumbPipe.push(tempTop);
					}
				}
			}
			
			// 4. Redraw all buttons
			var insertPoint:Number;
			var offset:Number;
			var offsetBack:Number;
			
			if(direct == "left"){
				offsetBack = overNumber * ( - _thumbSpace);
			}else if(direct == "right"){
				offsetBack = overNumber * (_thumbSpace);
			}	
					
			offset = distance;
			insertPoint = Number(headThumbX + offset + offsetBack);
			
			// If there is any thumb overlaped, array is changed, so have to refresh the list
			if(overNumber != 0){				
				for( i = 0; i < thumbNum; i++ ){
					removeChild(_thumbPipe[i]);
				}
				for( i = 0; i<thumbNum; i++ ){
					addChild(_thumbPipe[i]);
				}
			}
			
			// Caculate top/bottom inner thumb seq
			left = 0;
			right = _thumbSpace*_visibleNum - (_thumbSpace - _thumbWidth);
			
			// Move elements in list
			for( i = 0; i<thumbNum; i++ ){
				_thumbPipe[i].x = insertPoint;
				insertPoint += _thumbSpace;
//				size = Math.abs(bottom/2 - _thumbs[i].y)/1000;
//				Thumb(_thumbs[i]).size = size;
			}
			
			for( i = 0; i < thumbNum; i++ ){
				if((_thumbPipe[i].x - _thumbWidth/2) >= left ){
					_leftBoundSeq = i;
					break;
				};
			}
			var thumbRight:Number;
			for( i = 0; i < thumbNum; i++ ){
				thumbRight = _thumbPipe[i].x + _thumbWidth/2
				if( thumbRight > right ){
					_rightBoundSeq = i-1;
					break;
				};
			}
//			trace("_leftBoundSeq = " + _leftBoundSeq + "    " + "_rightBoundSeq = " + _rightBoundSeq);
			
			// 因为这里的变化都是一步到位，所以没有Tween;之所以要回移，就是为了在父级里面用Tween
			this.x += distance * (-1);
			
			// 5. Tell the others (目前而言，在上一层的CircleList中并未处理 Update)
//			dispatchEvent(new CircleListEvent(CircleListEvent.UPDATE_COMPLETE));
		}
		
		private function ClearThumb():void{
			var delThumb:ThumbH;
			while(_thumbPipe.length > 0){
				delThumb = _thumbPipe.pop();
				delThumb.Dispose();
			}
			_thumbPipe.length = 0;
		}
		
		public function FillList(itemList:ItemGroup):void{
			ClearThumb();
			
			_leftBoundSeq = 0;			
			var insertPosX:Number = 0 + _thumbWidth/2;
			var i:uint;
			var len:uint = itemList.Length;			
			for( i = 0; i<len; i++ ){
				var thumbVO:ThumbHVO = itemList.Items[i] as ThumbHVO;
				var thumb:ThumbH = new ThumbH(thumbVO);
				_thumbPipe.push(thumb);
				addChild(thumb);
				thumb.x = insertPosX;
				insertPosX += _thumbSpace;
			}
			
			// Relocate position
			MoveList(0);
		}
		
		// GET AND SET /////////////////
		public function get leftBoundSeq():int		{		return _leftBoundSeq;		}
		public function get rightBoundSeq():int	{		return _rightBoundSeq;		}
		public function get thumbPipe():Array		{		return _thumbPipe;			}
		
		public function Dispose():void				{		ClearThumb();				}
	}
}